﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using MaxAlarm.Common.Data;
using MaxAlarm.Core.Settings;

namespace MaxAlarm.Core
{
    public class MaxAlarmCore : DependencyObject
    {
        #region Singletone

        private static readonly object Locker = new object();
        private static MaxAlarmCore _instance;

        public static MaxAlarmCore Instance
        {
            get
            {
                lock (Locker)
                {
                    return _instance ?? (_instance = new MaxAlarmCore());
                }
            }
        }

        private MaxAlarmCore()
        {
            
        }

        #endregion

        #region Properties

        private BackgroundWorker _worker;

        public static readonly DependencyProperty ItemsProperty =
            DependencyProperty.Register("Items", typeof (ObservableCollection<AlarmItem>), typeof (MaxAlarmCore));

        public ObservableCollection<AlarmItem> Items
        {
            get { return (ObservableCollection<AlarmItem>) GetValue(ItemsProperty); }
            set { SetValue(ItemsProperty, value); }
        }

        public static readonly DependencyProperty RefreshingProperty =
            DependencyProperty.Register("Refreshing", typeof (bool), typeof (MaxAlarmCore), new PropertyMetadata(default(bool)));

        public bool Refreshing
        {
            get { return (bool) GetValue(RefreshingProperty); }
            set { SetValue(RefreshingProperty, value); }
        }

        public static readonly DependencyProperty IsBusyProperty =
            DependencyProperty.Register("IsBusy", typeof (bool), typeof (MaxAlarmCore), new PropertyMetadata(default(bool)));

        public bool IsBusy
        {
            get { return (bool) GetValue(IsBusyProperty); }
            set { SetValue(IsBusyProperty, value); }
        }

        public static readonly DependencyProperty PausedProperty =
            DependencyProperty.Register("Paused", typeof (bool), typeof (MaxAlarmCore), new PropertyMetadata(default(bool)));

        public bool Paused
        {
            get { return (bool) GetValue(PausedProperty); }
            private set { SetValue(PausedProperty, value); }
        }

        public static readonly DependencyProperty StoppedProperty =
            DependencyProperty.Register("Stopped", typeof (bool), typeof (MaxAlarmCore), new PropertyMetadata(default(bool)));

        public bool Stopped
        {
            get { return (bool) GetValue(StoppedProperty); }
            set { SetValue(StoppedProperty, value); }
        }

        #endregion

        #region Events

        public event EventHandler ItemsRefreshed;

        public void OnItemsRefreshed()
        {
            EventHandler handler = ItemsRefreshed;
            if (handler != null) handler(this, EventArgs.Empty);
        }

        #endregion

        #region Initialization

        public void Initialize()
        {
            Items = new ObservableCollection<AlarmItem>();
            _worker = new BackgroundWorker {WorkerSupportsCancellation = true};
            _worker.DoWork += DoWork;
            _worker.RunWorkerAsync();
        }

        void DoWork(object sender, DoWorkEventArgs e)
        {
            while(!_worker.CancellationPending && !Stopped)
            {
                if(Paused)
                {
                    Thread.Sleep(2000);
                    continue;
                }



                Thread.Sleep(SettingsManager.Instance.Settings.AutoRefreshInterval * 1000);
            }
        }

        #endregion

        #region Async work



        #endregion

        #region Controlling

        public void Run()
        {
            if (!IsBusy && !_worker.IsBusy)
            {
                Stopped = false;
                Paused = false;
                IsBusy = true;
                _worker.RunWorkerAsync();
            }
        }

        public void Pause()
        {
            Stopped = false;
            Paused = true;
            IsBusy = false;
        }

        public void Stop()
        {
            if (IsBusy || _worker.IsBusy)
            {
                Stopped = true;
                IsBusy = false;
                Paused = false;
                _worker.CancelAsync();
            }
        }

        #endregion
    }
}
